﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using WebUI.Controls;

namespace WebUI.EditPages
{
    public partial class TransferHistViewer : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            cldExToCreatedDate.TodaysDateFormat = SystemVariables.SysVars.DateFormat;
            cldExFromCreatedDate.TodaysDateFormat = SystemVariables.SysVars.DateFormat;

            cldExToCreatedDate.Format = SystemVariables.SysVars.DateFormat;
            cldExFromCreatedDate.Format = SystemVariables.SysVars.DateFormat;

            if (DateFrom == null || DateTo == null)
            {
                DateFrom = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                DateTo = DateTime.UtcNow.AddHours(SysVars.TimeZone);
            }

            cldExToCreatedDate.SelectedDate = DateTo;
            cldExFromCreatedDate.SelectedDate = DateFrom;
            
            if (!IsPostBack)
            {
                LoadPurchaseOrdersList();
            }
        }

        protected void btnSearchByNO_OnClick(object sender, EventArgs e)
        {
 
        }

        DateTime DateFrom
        {
            get
            {
                if (txtDateFrom.Text == "")
                    txtDateFrom.Text = DateTime.UtcNow.AddHours(SysVars.TimeZone).ToString(cldExFromCreatedDate.Format);
                return DateTime.ParseExact(txtDateFrom.Text, cldExFromCreatedDate.Format, null);
            }
            set { txtDateFrom.Text = value.ToString(cldExFromCreatedDate.Format); }
        }

        DateTime DateTo
        {
            get
            {
                if (txtDateTo.Text == "")
                    txtDateTo.Text = DateTime.UtcNow.AddHours(SysVars.TimeZone).ToString(cldExToCreatedDate.Format);
                return DateTime.ParseExact(txtDateTo.Text, cldExToCreatedDate.Format, null);
            }
            set { txtDateTo.Text = value.ToString(cldExToCreatedDate.Format); }
        }

        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            View_Transfers viewTf = null;


            if (DateFrom == null || DateTo == null)
                viewTf = View_Transfers.GetRelatedTransfers(Branches.CurrentBranch.ID);
            else
            {
                DateTime dateFrom = DateFrom;
                DateTime dateTo = DateTo;
                dateTo = dateTo.AddDays(1);
                dateTo = dateTo.AddSeconds(-1);

                viewTf = View_Transfers.GetRelatedTransfers(Branches.CurrentBranch.ID, dateFrom, dateTo);
                viewTf.Sort = View_Transfers.ColumnNames.TransferedDate + " desc";
            }
            //ivc.Sort = View_Invoices.ColumnNames.CreatedDate + " desc";
            tfHistoryList.SourceObject = viewTf;
            tfHistoryList.SortExpression = View_Transfers.ColumnNames.TransferedDate;
            tfHistoryList.SortDirection = BaseListControl.SORT_DESC;
            tfHistoryList.Refresh();
            
        }

        private void LoadPurchaseOrdersList()
        {
            View_PurchaseOrders viewPurchaseHist = View_PurchaseOrders.GetPurchaseOrderByID(0);
        }
    }
}